const express = require('express'),
    router = express.Router(),
    Api = require('../models/api.js'),
    apix = new Api(),
    crypto = require('crypto'),
    TITLE_LOGIN = '登录';

router.get('/',(req, res) => {
    res.render('login',{title:TITLE_LOGIN });
});

router.post('/',(req, res) => {
    let userName = req.body['txtUserName'],
        userPwd = req.body['txtUserPwd'],
        isRem = req.body['chbRem'],
        md5 = crypto.createHash('md5');

    apix.getUserByUserName(userName,results => {
        if(results == ''){
            res.locals.error = '用户不存在';
            res.render('login',{title:TITLE_LOGIN});
            return;
        }
        userPwd = md5.update(userPwd).digest('hex');
        if(results[0]['UserName'] != userName || results[0]['UserPass'] != userPwd){
            res.locals.error = '用户名或密码有误';
            res.render('login',{title:TITLE_LOGIN});
            return;
        }
        else{
            if(isRem){
                res.cookie('islogin',{userName:userName,isAdmin:results[0].isAdmin,userid:results[0].id} , { maxAge: 604800000 });
            }
            req.session.islogin = userName;
            req.session.isAdmin = results[0].isAdmin;
            req.session.userid = results[0].id;
            res.redirect('/blog');
            return;
        }
    });
});

module.exports = router;